var currentDepId;
var staffList;
var currentDepName;
var checkId;
$(document).ready(function (){
    var  depList= $('#depList');
    staffList=$('#staffList');
    $('.xh-datagrid-toolbar').parseToolbar();

    staffList.initDataGrid(moduleUrl + 'staff/getResource')
        .setSelectThenEnable(['bt_list_del','bt_list_move','bt_list_group'], ['bt_list_see']);
    $('#bt_list_add').setButtonDisable('eu');



    xhTool.bindDocKeyDown($('#bt_list_search'));
    $btn_menu1=$('#bt_list_state').menubutton({
        iconCls: 'fa fa-user',
        menu: '#mm'
    });

    $($btn_menu1.menubutton('options').menu).menu({
        onClick: function (item) {
            //item 的相关属性参见API中的menu
            var status=0;
            if(item.text=='未录取'){
                status=1;
            }else if(item.text=='待入职'){
                status=2;
            }else if(item.text=='未入职'){
                status=3;
            }else if(item.text=='入职'){
                status=4;
            }
            var id = staffList.getDataGridSelectedField('iStaffId');
            if(id==''){
                window.top.xhTop.showError('错误','请选择一个人');
                return false;
            }

            window.top.xhTop.showModal({
                title:item.text,
                content: '<iframe src="'+moduleUrl+'staff/stateForm/'+status+'/'+id+'"  style="width: 100%;;border-style: none;padding: 0px;height: 270px"  scrolling="yes"   ></iframe>',
                button:'提交',
                backClose: false,
            });

        }
    });

    $('#bt_list_see').click(function () {
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        var id = staffList.getDataGridSelectedField('iStaffId');
        if(id==''){
            window.top.xhTop.showError('错误','请选择一个查看的人');
            return false;
        }
        window.top.xhTop.showModal({
            title:'查看',
            content: '<iframe src="'+moduleUrl+'staff/stateTable/'+id+'"  style="width: 100%;;border-style: none;padding: 0px;height: 320px"  scrolling="yes"   ></iframe>',
            size:'lg',
            backClose: false
        });
    });

    $('#cc').resizable({    //左侧拖曳
        onStopResize:function (e) {
            depList.treegrid('resize',{width:e['data']['width']});
            $('.xh-datagrid').attr('width',($(document).width()-e['data']['width']));
            staffList.datagrid('resize',{width:($(document).width()-e['data']['width'])})
        },
        onResize:function (e) {
            depList.treegrid('resize',{width:e['data']['width']}+'px');
            staffList.datagrid('resize',{width:($(document).width()-e['data']['width'])})
        }
    });




    depList.treegrid({
        selectOnCheck:false,
        checkOnSelect:false,
        onSelect:function (row) {
            currentDepId=row['iDepId'];
            currentDepName=row['sDepName'];
            depList.treegrid('expand',currentDepId);
            $('#bt_list_add').setButtonEnable('eu');
            $('#bt_list_import').setButtonEnable('eu');
            staffList.addDataGridParams(getParams()).datagrid('getPanel').panel('setTitle', '职工列表（' + row['sDepName'] + '）');
        },
        onBeforeCheck:function (row) {
            if(checkId==undefined){
                checkId = row['iDepId'];
                return true;
            }else {
                if(checkId==row['iDepId']){
                    depList.treegrid('uncheckRow',row['iDepId']);
                    return false;
                }else {
                    depList.treegrid('uncheckRow',checkId);
                }
            }
        },
        onCheck:function (row) {
            checkId = row['iDepId'];
        },
        onLoadSuccess:function (row, data) {
            depList.treegrid('select',0);
        }

    });

    DataGrid.onLoadSuccess = function(data){
        staffList.dataGridClear();
    };


    $('#bt_list_del').click(function () {
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        var ids = staffList.getDataGridSelectedFields('iStaffId');
        $.overtFun.clickDelList(ids, moduleUrl + 'staff/delStaff/resource',staffList);
    });

    $('#bt_list_search').click(function () {
        var key=xhExtra.getValue($('#list_search'));
        var state=xhExtra.getValue($('#select_state'));
        staffList.addDataGridParams({key:key,depId:currentDepId,state:state});
    });



    $('#bt_list_move').click(function () {
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        var $depArray=depList.treegrid('getChecked');
        if($depArray.length>0){
            var depId=$depArray[0]['iDepId'];
            var depName=$depArray[0]['sDepName'];
            var staffIds=staffList.getDataGridSelectedFields('iStaffId');
            var staffName=staffList.getDataGridSelectedFields('sStaffName');
            window.top.swal({
                title: '添加人员到部门',
                text: '你确定要将'+staffName+'移动到'+depName+"部门",
                type: 'warning',
                showCancelButton: true,
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(function() {
                $.ajax({
                    url:moduleUrl+'staff/moveGroup/resource',
                    data:{depId:depId,staffIds:staffIds},
                    type:'post',
                    success:function (data) {

                        staffList.addDataGridParams({depId:depId}).datagrid('getPanel').panel('setTitle', '职工列表（' + $depArray[0]['sDepName'] + '）');
                        staffList.dataGridClear();
                        window.top.swal(
                            '添加成功',
                            ''+staffName+'已成功添加到'+depName+"部门",
                            'success'
                        )
                    },
                    error:function (data) {
                        console.log(data);
                        window.top.swal(
                            '失败',
                            data['responseText'],
                            'error'
                        );
                    }

                })

            }, function(dismiss) {
                // dismiss can be 'overlay', 'cancel', 'close', 'esc', 'timer'
                if (dismiss === 'cancel') {
                    window.top.swal(
                        '取消',
                        '取消添加',
                        'error'
                    )
                }
            });
        }else {
            alert('请选择部门');
        }
    });


    DataGrid.onDblClickRow=function (rowIndex, rowData) {
        var id=rowData['iStaffId'];
        var url='iDepId='+currentDepId+'&sDepName='+escape(currentDepName);
        window.top.xhTop.showModal({
            title:'编辑职工',
            content: '<iframe src="'+moduleUrl+'staff/resourceForm/'+id+'?'+url+'"  style="width: 100%;;border-style: none;padding: 0px;height: 420px"  scrolling="yes"   ></iframe>',
            button:'提交',
            size: 'lg',
            backClose: false
        });
    };


    $('#bt_list_add').click(function () {
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        window.frames['tabFrame'].location.href = baseUrl+'school_book/schTab/'+rowData['iSBLId'];
        $this.titleRow.show();
        $this.tabRow.show();
        xhExtra.forwardToForm(true);
    });



});

function getParams() {
    var key=xhExtra.getValue($('#list_search'));
    var all=xhExtra.getValue($('#select_all'));
    var workstate=xhExtra.getValue($('#select_work'));
    var staffstate=xhExtra.getValue($('#select_staff'));
    var offerstate=xhExtra.getValue($('#select_offer'));
    return {key:key,all:all,workstate:workstate,staffstate:staffstate,offerstate:offerstate,depId:currentDepId};
}